﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EazyCMS.Common;

namespace EazyCMS.Web.Plugin.Naire.admin
{
    public partial class vote_edit : EazyCMS.Web.UI.ManagePage
    {
        private string action = EazyEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        private int pid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = EazyRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == EazyEnums.ActionEnum.Edit.ToString())
            {
                this.action = EazyEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = EazyRequest.GetQueryInt("id");
                this.pid = EazyRequest.GetQueryInt("pid");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.vote().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            else
            {
                this.pid = EazyRequest.GetQueryInt("pid");

            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("plugin_vote", EazyEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind(); //绑定站点
                if (action == EazyEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定站点=================================
        private void TreeBind()
        {
        }

     
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.vote bll = new BLL.vote();
            Model.vote model = bll.GetModel(_id);

           
            txtSortId.Text = model.sort_id.ToString();
          
            txtTitle.Text = model.title;
            rblIsMulti.SelectedValue = model.is_multi.ToString();
            txtStartTime.Text = model.start_time.ToString("yyyy-MM-dd");
            txtEndTime.Text = model.end_time.ToString("yyyy-MM-dd");
            txtContent.Value = model.content;
            //绑定投票选项
            rptList.DataSource = model.options;
            rptList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.vote model = new Model.vote();
            BLL.vote bll = new BLL.vote();

           
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.title = txtTitle.Text.Trim();
            model.is_multi = Utils.StrToInt(rblIsMulti.SelectedValue, 0);
            model.start_time = DateTime.Parse(this.txtStartTime.Text.Trim());
            model.end_time = DateTime.Parse(this.txtEndTime.Text.Trim());
            model.content = txtContent.Value;
          
            model.naire_id = this.pid;

            #region 保存投票选项
            string[] itemIdArr = Request.Form.GetValues("item_id");
            string[] itemTitleArr = Request.Form.GetValues("item_title");
            string[] itemNumArr = Request.Form.GetValues("item_num");
            string[] itemSortIdArr = Request.Form.GetValues("item_sortid");
            if (itemIdArr != null && itemTitleArr != null && itemNumArr != null && itemSortIdArr != null)
            {
                if ((itemIdArr.Length == itemTitleArr.Length) && (itemTitleArr.Length == itemNumArr.Length) && (itemNumArr.Length == itemSortIdArr.Length))
                {
                    List<Model.vote_option> ls = new List<Model.vote_option>();
                    for (int i = 0; i < itemIdArr.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(itemIdArr[i].Trim()))
                        {
                            Model.vote_option modelt = new Model.vote_option();
                            modelt.id = Utils.StrToInt(itemIdArr[i].Trim(), 0);
                            modelt.title = itemTitleArr[i].Trim();
                            modelt.vote_num = Utils.StrToInt(itemNumArr[i].Trim(), 0);
                            modelt.sort_id = Utils.StrToInt(itemSortIdArr[i].Trim(), 99);
                            ls.Add(modelt);
                        }
                    }
                    model.options = ls;
                }
            }
            #endregion
            
            if (bll.Add(model) > 0)
            {
                AddAdminLog(EazyEnums.ActionEnum.Add.ToString(), "添加调查题目：" + model.title); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.vote bll = new BLL.vote();
            Model.vote model = bll.GetModel(_id);

          
           
            model.title = txtTitle.Text.Trim();
            model.is_multi = Utils.StrToInt(rblIsMulti.SelectedValue, 0);
            model.start_time = DateTime.Parse(this.txtStartTime.Text.Trim());
            model.end_time = DateTime.Parse(this.txtEndTime.Text.Trim());
            model.content = txtContent.Value;
          
            //model.special_id = Convert.ToInt32(ddspecial.SelectedValue);
            #region 保存投票选项
            string[] itemIdArr = Request.Form.GetValues("item_id");
            string[] itemTitleArr = Request.Form.GetValues("item_title");
            string[] itemNumArr = Request.Form.GetValues("item_num");
            string[] itemSortIdArr = Request.Form.GetValues("item_sortid");
            if (itemIdArr != null && itemTitleArr != null && itemNumArr != null && itemSortIdArr != null)
            {
                if ((itemIdArr.Length == itemTitleArr.Length) && (itemTitleArr.Length == itemNumArr.Length) && (itemNumArr.Length == itemSortIdArr.Length))
                {
                    List<Model.vote_option> ls = new List<Model.vote_option>();
                    for (int i = 0; i < itemIdArr.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(itemIdArr[i].Trim()))
                        {
                            Model.vote_option modelt = new Model.vote_option();
                            modelt.id = Utils.StrToInt(itemIdArr[i].Trim(), 0);
                            modelt.vote_id = model.id;
                            modelt.title = itemTitleArr[i].Trim();
                            modelt.vote_num = Utils.StrToInt(itemNumArr[i].Trim(), 0);
                            modelt.sort_id = Utils.StrToInt(itemSortIdArr[i].Trim(), 99);
                            ls.Add(modelt);
                        }
                    }
                    model.options = ls;
                }
            }
            #endregion

            if (bll.Update(model))
            {
                AddAdminLog(EazyEnums.ActionEnum.Edit.ToString(), "修改投票主题：" + model.title); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == EazyEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("plugin_vote", EazyEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("修改在线投票成功！", "index.aspx?id="+this.pid);
            }
            else //添加
            {
                ChkAdminLevel("plugin_vote", EazyEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加在线投票成功！", "index.aspx?id="+this.pid);
            }
        }

    }
}